-- DROP FUNCTION User_Info_Audit();

CREATE OR REPLACE FUNCTION User_Info_Audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO User_Info_Audit SELECT nextval('User_Info_Audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO User_Info_Audit SELECT nextval('User_Info_Audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO User_Info_Audit SELECT nextval('User_Info_Audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION User_Info_Audit() OWNER TO postgres;

CREATE TRIGGER User_Info_Audit
    AFTER INSERT OR DELETE OR UPDATE ON user_info
    FOR EACH ROW
    EXECUTE PROCEDURE User_Info_Audit();